import { pick, getDeviceType } from '@/utils/helper';

function pickAppUserData(obj) {
  const props = pick([
    'uuid',
    'device_id',
    'device_code',
    'device_model',
    'device_type',
    'app_version',
    'lan',
    'token',
    'debug',
    'memberId'
  ]);
  const data = props(obj);
  if (!data.device_type) data.device_type = getDeviceType();
  return data;
}

function serializeJsonAppUserData(string) {
  try {
    const obj = JSON.parse(string);
    return pickAppUserData(obj);
  } catch (e) {
    return {};
  }
}

function getAppUserDataLocalStorage() {
  try {
    return localStorage.getItem('userData');
  } catch (e) {
    console.error(e);
    return '';
  }
}

export { serializeJsonAppUserData, getAppUserDataLocalStorage };
